package irc.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import irc.view.*;


/**
 * This class manages the the menubar
 * This class contains the actionListeners to the menu-bar.
 * @author Jo
 * @since 1.6
 * @version 3.7
 */
public class MenuController {
		public DesktopView desktop;
		public String server;
		public JFrame frame;
		public BaseController CC;
		public MenuView menu;
		private ConnectionController CnC;
		
		
		/**
		 * Constructor. It initializes MainView and ConnectionView, and get together
		 * a frame
		 * @since 1.6
		 * @version 2.0
		 */
		public MenuController(BaseController CC, ConnectionController CnC)	{
			menu = new MenuView(this);
			this.CnC = CnC; // sets the two controllers
			this.CC = CC;
			CnC.getMC(this); //sends menucontroller to basecontroller and connectioncontroller
			CC.getMC(this);
		}
		
		
		/**
		 * ActionListeners for filebutton.
		 * @author Dybdal
		 * @since 1.4
		 * @version 1.5
		 */
		public void fileActionlisteners(final JMenuItem connect, final JMenuItem disConnect, JMenuItem allChannels, JMenuItem joinChan, JMenuItem exit){
			  
			  // connect
			  connect.addActionListener(new ActionListener(){
		 		 public void actionPerformed(ActionEvent ae){	
		 			CnC.showFrame(); // shows the frame
		 		 }
		 	  });

			  // disconnect
			  disConnect.addActionListener(new ActionListener(){
		 		 public void actionPerformed(ActionEvent ae){
		 			CC.closeConnection(); // closing the connection
		 		 }
		 	  });	
			  
			  // See all channels
			  allChannels.addActionListener(new ActionListener(){
		 		 public void actionPerformed(ActionEvent ae){
		 			 CC.seeAllChannels();	// shows all the channels available
		 		 }
		 	  });
			  
			  // join channel
			  joinChan.addActionListener(new ActionListener(){
		 		 public void actionPerformed(ActionEvent ae){
		 			String chan = JOptionPane.showInputDialog ("Channel:");
		 			if(chan != null)
		 				CC.joinChannel("#"+chan);	// join channel whit a joptionpane
		 		 }
		 	  });	
			  
			 // exit program 
			 exit.addActionListener(new ActionListener(){
			 		 public void actionPerformed(ActionEvent ae){
			 			if (JOptionPane.showConfirmDialog(null,"Are you sure you want to quit?\n")==JOptionPane.YES_OPTION)	{
		            		if(CC.isConnected){
		            		CC.closeConnection();
		            		}
		            		System.exit(0);
		            	}
			 		 }
			 	  });
		
		}
		/**
		 * ActionListeners for editbutton.
		 * @author Dybdal
		 * @since 1.4
		 * @version 1.5
		 */
		public void editActionlisteners(JMenuItem font){
			font.addActionListener(new ActionListener(){
		 		 public void actionPerformed(ActionEvent ae){	
		 			 CC.fc.fonts.openFontOptions();	// opens the font option
		 		 }
		 	  });
		}
		
		/**
		 * ActionListeners for helpbutton.
		 * @author Dybdal
		 * @since 1.4
		 * @version 1.5
		 */
		public void helpActionlisteners(JMenuItem help){
			help.addActionListener(new ActionListener(){
		 		 public void actionPerformed(ActionEvent ae){	
		 			 CC.helpMessage();	// shows help message
		 		 }
		 	  });
		}

		/**
		 * hides the disconnectbutton in file
		 * @author Dybdal
		 * @since 1.4
		 * @version 1.5
		 */
		public void hideDisConnect(){
			menu.hideDisConnect();	// hides the disconnect button
		}

		/**
		 * hides the connectbutton in file
		 * @author Dybdal
		 * @since 1.4
		 * @version 1.5
		 */
		public void hideConnect() {
			menu.hideConnect();		// hides the connect button
		}
		
}